﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Security;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;

namespace TD
{
    public class SoundManager : ManagerBase<SoundEffect>
    {
        public SoundManager(Game game) :
            base(game)
        {
            // Do something...
        }

        protected override SoundEffect GetDefault()
        {
            return base.Game.Content.Load<SoundEffect>("audio\\sounds\\null");
        }

        protected override SoundEffect LoadContent(string assetName)
        {
            try
            {
                string path = (Environment.CurrentDirectory + "\\audio\\sounds\\" + assetName + ".wav");
                if (File.Exists(path))
                {
                    FileAttributes attributes = File.GetAttributes(path);
                    if (attributes == FileAttributes.Archive)
                    {
                        Debug.WriteLine("Loading sound '" + assetName + "'...");

                        using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
                        {
                            return SoundEffect.FromStream(stream);
                        }
                    }
                    else
                    {
                        Debug.WriteLine("Discarded sound! File attributes mismatched for sound: " + assetName);
                    }
                }
                else
                {
                    Debug.WriteLine("Discarded sound! Did not find sound with name: " + assetName);
                }
            }
            catch (SecurityException)
            {
                Debug.WriteLine("Sound '" + assetName + "' has not been loaded due to insufficient clearance.");
            }
            catch (Exception ex)
            {
                Debug.WriteLine("An unknown error occured while loading '" + assetName + "' sound.");
#if DEBUG
                Debug.WriteLine(ex.GetType().ToString() + ": " + ex.Message);
#endif
            }

            return UseAsset("default");
        }

        public void Play(string assetName)
        {
            if (!IsPlaying(assetName))
            {
                SoundEffectInstance instance = UseAsset(assetName).CreateInstance();
                instance.Play();
            }
        }

        public void Loop(string assetName)
        {
            //SoundEffectInstance instance = UseAsset(assetName).CreateInstance();
            //instance.IsLooped = true;
            //Play(assetName);
        }

        public void Stop(string assetName)
        {
            //SoundEffectInstance instance = UseAsset(assetName).CreateInstance();
            //instance.Stop(true);
        }

        public bool IsPlaying(string assetName)
        {
            /*SoundEffectInstance instance = UseAsset(assetName).CreateInstance();
            return (instance.State == SoundState.Playing);*/
            return false;
        }
    }
}
